home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / strpchrt.zip / EX_TRACE.C < prev    next >
Text File  |  1993-07-10  |  8KB  |  324 lines

  1. /*
  2.    Strip Chart Library Multiple Trace Windows Example.
  3.    This program draws a user-specified number of traces.
  4.  
  5.    Copyright (c) 1993 by Christopher Lim.  All rights reserved.
  6. */
  7.  
  8. /*
  9.    Function func() simulates digitizing a sine wave.
  10.    SPS defined below is the simulated sampling rate.
  11.    The program can be made to run faster by using a lower sampling rate.
  12. */
  13. #define SPS 300.0
  14.  
  15.  
  16. #include "sc.h"
  17. #include <stdio.h>
  18. #include <math.h>
  19. #include <conio.h>
  20. #include <stdlib.h>
  21. #include <malloc.h>
  22.  
  23. #ifdef BORLAND
  24. #include <alloc.h> /* for coreleft() */
  25. #endif
  26.  
  27. #define MIN_YHEIGHT (0.25+0.125) /* minimum trace height */
  28. #define MAX_AMPLITUDE 1.0 /* max trace amplitude */
  29. #define MARGIN (1.375) /* space for title and time windows */
  30.  
  31. /*------------------------------------------------------------*/
  32.  
  33. int FreeMem(void)
  34. {
  35.    /* Prints out free memory.
  36.       This function is compiler dependent.
  37.     */
  38. #ifdef BORLAND
  39.     printf("Free memory: %ld\n",(unsigned long)coreleft());
  40. #endif
  41. #ifdef MICROSOFT
  42.    printf("Free memory: %ld\n",(unsigned long)_memavl());
  43. #endif
  44.     return 0;
  45. }
  46.  
  47. /*------------------------------------------------------------*/
  48.  
  49. double func(int n, double t)
  50. {
  51.    /* Test function response for time t for trace n.
  52.       To make the plot look more interesting, we will have
  53.       8 types of traces.
  54.       First four traces: sine waves with constant amplitude.
  55.       Next four traces: sine waves with periodic amplitudes.
  56.       Pattern repeats as above for each set of 8 traces.
  57.     */
  58.    double x;
  59.    double a; /* amplitude */
  60.    double freq; /* sine wave frequency in hertz */
  61.    double pi = 3.1416;
  62.  
  63.    switch (n%8) /* index into the set of 8 traces */
  64.    {
  65.       case 0:
  66.          a = MAX_AMPLITUDE;
  67.          freq = 2.0; /* 2 hz */
  68.          break;
  69.       case 1:
  70.          a = MAX_AMPLITUDE;
  71.          freq = 4.0; /* 4 hz */
  72.          break;
  73.       case 2:
  74.          a = MAX_AMPLITUDE;
  75.          freq = 8.0; /* 8 hz */
  76.          break;
  77.       case 3:
  78.          a = MAX_AMPLITUDE;
  79.          freq = 16.0; /* 16 hz */
  80.          break;
  81.       case 4:
  82.          a = cos(2 * pi * t * 0.5);
  83.          freq = 2.0; /* 2 hz */
  84.          break;
  85.       case 5:
  86.          a = cos(2 * pi * t * 0.5);
  87.          freq = 4.0; /* 4 hz */
  88.          break;
  89.       case 6:
  90.          a = cos(2 * pi * t * 0.5);
  91.          freq = 8.0; /* 8 hz */
  92.          break;
  93.       case 7:
  94.          a = cos(2 * pi * t * 0.5);
  95.          freq = 16.0; /* 16 hz */
  96.          break;
  97.    }
  98.    x = a * sin(2 * pi * t * freq);
  99.    return(x);
  100. }
  101.  
  102. /*------------------------------------------------------------*/
  103.  
  104. void GetPrinterType(unsigned char *printerType, unsigned char *printerRes,
  105.    char **s)
  106. {
  107.    int c;
  108.    unsigned char i,j;
  109.  
  110.    printf("Select printer type and resolution code: \n");
  111.    printf("[1] 9 PIN LOW RES\n");
  112.    printf("[2] 9 PIN HIGH RES\n");
  113.    printf("[3] 24 PIN LOW RES\n");
  114.    printf("[4] 24 PIN HIGH RES\n");
  115.    printf("Select code [1,2,3 or 4]: ");
  116.    c = getch();
  117.    printf("%c\n",c);
  118.    switch (c)
  119.    {
  120.         case '1': i = SC_9PIN; j = SC_LOW_RES;
  121.              *s = "9 PIN LOW RES";
  122.              break;
  123.         case '2': i = SC_9PIN; j = SC_HIGH_RES;
  124.              *s = "9 PIN HIGH RES";
  125.              break;
  126.         case '3': i = SC_24PIN; j = SC_LOW_RES;
  127.              *s = "24 PIN LOW RES";
  128.              break;
  129.         case '4': i = SC_24PIN; j = SC_HIGH_RES;
  130.              *s = "24 PIN HIGH RES";
  131.              break;
  132.         default:
  133.              printf("Aborted\n");
  134.              exit(1);
  135.    }
  136.    printf("%s\n",*s);
  137.    *printerType = i;
  138.    *printerRes = j;
  139.    return;
  140. }
  141. /*------------------------------------------------------------*/
  142.  
  143. void GetPlotParms(double *plotWidth, double *plotDuration, int *ntraces)
  144. {
  145.    /* Get plot parameters from user */
  146.    int maxtraces;
  147.    char buf[80];
  148.  
  149.    while (1)
  150.    {
  151.       printf("Enter plot or paper width (inches): ");
  152.       gets(buf);
  153.       *plotWidth = atof(buf);
  154.       if (*plotWidth < 4.0 || *plotWidth > 13.6)
  155.          printf("\nPlot Width must be greater than 4.0 and less than 13.6 inches!\n");
  156.       else
  157.          break;
  158.    }
  159.    printf("Plot width = %f inches\n", *plotWidth);
  160.    
  161.    while (1)
  162.    {
  163.       printf("Enter plot duration (inches): ");
  164.       gets(buf);
  165.       *plotDuration = atof(buf);
  166.       if (*plotDuration <= 0)
  167.          printf("\nPlot Duration must be a positive number!\n");
  168.       else
  169.          break;
  170.    }
  171.    printf("Plot duration = %f seconds\n",*plotDuration);
  172.  
  173.    maxtraces = (*plotWidth-MARGIN)/MIN_YHEIGHT;
  174.    while (1)
  175.    {
  176.       printf("Enter number of traces [max=%d]: ",maxtraces);
  177.       gets(buf);
  178.       *ntraces = atoi(buf);
  179.       if (*ntraces <= 0 || *ntraces > maxtraces)
  180.          printf("\nNumber of traces must be greater than zero and less than %d!\n",maxtraces+1);
  181.       else
  182.          break;
  183.    }
  184.    printf("Number of traces = %d\n",*ntraces);
  185. }
  186.  
  187. /*------------------------------------------------------------*/
  188.  
  189. char *Label(SC_WINDOW *w, double seconds, unsigned long labelCount)
  190. {
  191.    static char buf[80];
  192.    static int minutes = 0;
  193.    static int hours = 0;
  194.  
  195.    while (seconds >= 60.0)
  196.    {
  197.       seconds -= 60.0;
  198.       minutes++;
  199.    }
  200.    if (minutes >= 60)
  201.    {
  202.       minutes %= 60;
  203.       hours += minutes/60;
  204.    }
  205.    sprintf(buf,"%02d:%02d:%05.2f",hours,minutes,seconds);
  206.    return buf;
  207. }
  208.  
  209. /*------------------------------------------------------------*/
  210.  
  211. void ErrorTrap(char *msg)
  212. {
  213.     printf("%s\n",msg);
  214.     exit(1);
  215. }
  216.  
  217. /*------------------------------------------------------------*/
  218.  
  219. void main()
  220. {
  221.    int i,j;
  222.    int error;
  223.    double scan;
  224.    double a;
  225.    SC_WINDOW *text, *time;
  226.    SC_WINDOW **trace;
  227.    double sps = SPS; /* simulated sampling rate */
  228.    double t;
  229.    unsigned char printerType, printerRes;
  230.    char string[128];
  231.    char *s;
  232.    double plotWidth;
  233.    double plotDuration;
  234.    int ntraces;
  235.    double yheight;
  236.  
  237.    if (!SC_PrinterReady(LPT1))
  238.    {
  239.       printf("Printer at LPT1 is not ready!\n");
  240.       exit(1);
  241.    }
  242.  
  243.    GetPrinterType(&printerType,&printerRes,&s);
  244.    GetPlotParms(&plotWidth,&plotDuration,&ntraces);
  245.  
  246.    FreeMem();
  247.  
  248.    /* Allocate trace window array */
  249.    trace = (SC_WINDOW **)malloc(sizeof(SC_WINDOW *) * ntraces);
  250.    if (trace == NULL)
  251.    {
  252.       printf("Insufficient memory.\n");
  253.       exit(1);
  254.    }
  255.  
  256.    /* Error trap */
  257.    SC_SetErrorTrap(ErrorTrap);
  258.  
  259.    /* Initialize: 8.5 inch paper, plot speed = 1 inch per second */
  260.    SC_Init(LPT1, printerType,printerRes, INCHES, plotWidth, 1.0,&error);
  261.  
  262.    /* Horizontal Grids: 2 divisions */
  263.    SC_SetHorizontalGrids(2,&error);
  264.  
  265.    /* Time grids every 1 second */
  266.    SC_SetTimeGrids(1.0,&error);
  267.  
  268.    /* Text window to display title */
  269.    text = SC_AddTextWindow(0.5,14.0/72.0,&error);
  270.  
  271.    /* Add trace windows */
  272.    yheight = (plotWidth-MARGIN)/ntraces - 0.125;
  273.    for (i=0; i < ntraces; i++)
  274.    {
  275.       trace[i] = SC_AddTraceWindow(SC_yoffset+0.125, /* window offset */
  276.          yheight, /* window height */
  277.          -1.0*MAX_AMPLITUDE, MAX_AMPLITUDE,  /* window limits */
  278.          SCF_BORDER|SCF_GRID, /* enable borders and grids */
  279.          &error);
  280.    }
  281.  
  282.    /* Add time axis at the bottom of the plot */
  283.    time = SC_AddTimeWindow(SC_yoffset+0.125, /* window offset */
  284.       20.0/72.0, /* window height */
  285.       LABELS_BELOW_AXIS, /* labels below axis */
  286.       2.0, /* label every 2 seconds */
  287.       5, /* minor divisions */
  288.       Label, /* call back function */
  289.       &error);
  290.  
  291.  
  292.    /* initialize text windows */
  293.    sprintf(string,"STRIP CHART LIBRARY EXAMPLES: %d TRACES (%s)",ntraces, s);
  294.    SC_Text(text,string,&error);
  295.  
  296.    /* draw traces */
  297.    a = 1.0; /* initial amplitude */
  298.    for (t=0.0; t < plotDuration; t += 1.0/sps)
  299.    {
  300.       if (kbhit())
  301.       {
  302.          printf("User aborted.\n");
  303.          break;
  304.       }
  305.  
  306.       for (i=0; i < ntraces; i++)
  307.       {
  308.          scan = a * func(i,t);
  309.          SC_Trace(trace[i],scan,&error);
  310.       }
  311.  
  312.       SC_Draw();
  313.       SC_Advance(1.0/sps); /* next */
  314.    }
  315.  
  316.    SC_Close();
  317.    
  318.    free(trace);
  319.  
  320.    FreeMem();
  321. }
  322.  
  323.  
  324.